package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.city_home_app.Activity.MessageActivity;
import com.example.city_home_app.Bean.Down_MsgCencter;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.MyCircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_MsgCenter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_MsgCencter> list;

    public Adapter_MsgCenter(Context context) {
        this.context = context;
    }

    public void setList(List<Down_MsgCencter> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_msgcenter, parent, false);
        Adapter_MsgCenter.ViewHolder viewHolder = new Adapter_MsgCenter.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_MsgCencter bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_MsgCenter.ViewHolder viewHolder = (Adapter_MsgCenter.ViewHolder) holder;

        viewHolder.tvRecitemMsgcenterName.setText(bean.getUser_name());
        viewHolder.tvRecitemMsgcenterTime.setText(bean.getMsg_time());
        viewHolder.tvRecitemMsgcenterContent.setText(bean.getMsg_content());
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                .into(viewHolder.myCircleImageView);
        if (bean.getCount2() == 0) {
            viewHolder.tvRecitemMsgcenterCount.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.tvRecitemMsgcenterCount.setVisibility(View.VISIBLE);
            viewHolder.tvRecitemMsgcenterCount.setText(bean.getCount2() + "");
        }
        viewHolder.layoutRecitemMsgcenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MessageActivity.class);
                intent.putExtra("sendid",bean.getUser_id2());
                intent.putExtra("sendname",bean.getUser_name());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.myCircleImageView)
        MyCircleImageView myCircleImageView;
        @BindView(R.id.tv_recitem_msgcenter_name)
        TextView tvRecitemMsgcenterName;
        @BindView(R.id.tv_recitem_msgcenter_content)
        TextView tvRecitemMsgcenterContent;
        @BindView(R.id.tv_recitem_msgcenter_time)
        TextView tvRecitemMsgcenterTime;
        @BindView(R.id.tv_recitem_msgcenter_count)
        TextView tvRecitemMsgcenterCount;
        @BindView(R.id.layout_recitem_msgcenter)
        ConstraintLayout layoutRecitemMsgcenter;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
